// import { store } from '@/store'
import { useUserStoreWithOut } from '@/store/modules/user'

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function hasPermi(value: unknown): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const { roles, perms } = useUserStoreWithOut()
    // 超级管理员拥有所有权限
    if (roles.includes('anvil')) {
      return true
    }

    // const permissions = store.getters && store.getters.permissions
    const permissionDatas = value

    const hasPermission = perms.some((permission) => {
      return permissionDatas.includes(permission) || permission === '*:*:*'
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like hasPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function hasRole(value: unknown): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const { roles } = useUserStoreWithOut()
    // const roles = store.getters && store.getters.roles
    const permissionRoles = value

    const hasRole = roles.some((role) => {
      return permissionRoles.includes(role)
    })

    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like hasRole="['admin','editor']"`)
    return false
  }
}

/**
 * 角色类型权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function hasRoleType(value: unknown): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const { roleTypes } = useUserStoreWithOut()
    // const roleTypes = store.getters && store.getters.roleTypes
    const super_role = 'super'
    const permissionRoleTypes = value

    const hasRole = roleTypes.some((role) => {
      return super_role === role || permissionRoleTypes.includes(role)
    })

    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like hasRoleType="['admin','editor']"`)
    return false
  }
}
